const router = require('express').Router();
const {
    wx_pay
} = require('../../utils/wx');
const connection = require('./models/order');

router.post('/pay', async (req, res) => {
    const order = {};
    order.order_no = 'D' + Date.now();
    order.fee = req.body.fee;
    let sql = "INSERT INTO `order` (order_no,fee) VALUES ('"+order.order_no+"',"+ order.fee +")";
    connection.query(sql, function (error, results, fields) {
        if (error) throw error;
        console.log('The solution is: ', results);

    });
    const noncestr = Date().now();
    const sign = await wx_pay({
        body:'测试下单',
        orderNo:order.order_no,
        ip:'114.25.36.1',
        totalFee:order.fee ,
        nonceStr:noncestr
    })
    if(sign.xml.return_code == 'SUCCESS' && sign.xml.result_code == 'SUCCESS' ){
        res.json({
            code:1,
            info:sign.xml
        })
    }
})

module.exports = router;